#include "UIManager.h"

#include <QMessageBox>

#include <exception>

UIManager::UIManager()
{
    login = nullptr;
    dashboard = nullptr;

    showLogin();
}

UIManager::~UIManager()
{
    if (login != nullptr)
        delete login;

    if (dashboard != nullptr)
        delete dashboard;
}

void UIManager::showLogin()
{
    if (dashboard != nullptr)
    {
        dashboard->deleteLater();
        dashboard = nullptr;
    }

    try {
        login = new Login(this);
    } catch (std::bad_alloc &e) {
        QMessageBox::critical(NULL, "Memory Allocation Error", e.what());
    }
}

void UIManager::showDashboard()
{
    if (login != nullptr)
    {
        login->deleteLater();
        login = nullptr;
    }

    try {
        dashboard = new Dashboard(this);
    } catch (std::bad_alloc &e) {
        QMessageBox::critical(NULL, "Memory Allocation Error", e.what());
    }
}
